package ExerChapter13Wrapper;

import java.util.Arrays;
import java.util.Comparator;

public class Arrays_ {
    public static void main(String[] args) {
        Book[] books = new Book[4];

        books[0] = new Book("红楼梦1",100);
        books[1] = new Book("红楼梦2",77);
        books[2] = new Book("红楼梦3",66);
        books[3] = new Book("红楼梦4",99);
        System.out.println(Arrays.toString(books));
        // 内部类+动态绑定+向下转型
        Arrays.sort(books,new Comparator<Book>() {
            public int compare(Book n1,Book n2){
                Book i1 = (Book) n1;
                Book i2 = (Book) n2;
                return i1.getPrice() - i2.getPrice();
            }
        });

        System.out.println(Arrays.toString(books));

        System.out.println("==========================================");
        // 数组常用方法
        Book books2[] = Arrays.copyOf(books,4);
        boolean s1 = Arrays.equals(books, books2);
        System.out.println(s1);

        Arrays.fill(books, new Book("Nihao",95));
        System.out.println(Arrays.toString(books));

        int[] arr = {2,3,5,1,9,10};
        Arrays.sort(arr);

        System.out.println(Arrays.toString(arr));
        System.out.println(Arrays.binarySearch(arr, 3));
    }
}

class Book{
    private String name;
    private int price;

    public Book(String name, int price) {
        this.name = name;
        this.price = price;
    }

    public int getPrice() {
        return price;
    }


    public void setPrice(int price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book [name=" + name + ", price=" + price + "]";
    }
    
}
